package com.xperblueray.stepbystep.error;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

import java.util.HashSet;
import java.util.Set;

@Configuration
public class ErrorPageConfig {

    @Bean
    public WebServerFactoryCustomizer containerCustomizer() {

        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                Set<ErrorPage> errorPages = new HashSet<>();
                errorPages.add(new ErrorPage(HttpStatus.FORBIDDEN, "/403.html"));
                errorPages.add(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
                errorPages.add(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"));
//                ErrorPage error400Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
//                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
//                ErrorPage error500Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
                factory.setErrorPages(errorPages);
            }
        };
    }
}
